package suit.ui;

import java.util.Vector;

public class List extends Widget implements ListItem.Clicked {

	private int index;

	Rectangle getRequisition() {
		return new Rectangle(0, 0, 50, 16);
	}

	public List() {
		children = new Vector();
	}

	protected boolean onExpose(Event e) {
		e.theme.drawEntry(e.graphics, this, allocation);
		startClip(e.graphics);
		int size = children.size();
		int y = allocation.y;
		for (int i = 0; i < size; i++) {
			ListItem b = (ListItem) children.elementAt(i);
			int h = b.getRequisition().h;
			b.sizeAllocate(new Rectangle(allocation.x, y,
					allocation.w, h));
			b.onExpose(e);
			y = y + h;
		}
		return false;
	}

	public void add(String str) {
		ListItem i = new ListItem(new Label(str));
		i.connect(this);
		add(i);
	}

	public void remove(int index) {
		children.removeElementAt(index);
		invalidate(this);
	}

	public void clear() {
		children.removeAllElements();
		invalidate(this);
	}

	public Widget getSelected() {
		return (Widget) children.elementAt(index);
	}

	public void onClicked(ListItem source) {
		index = children.indexOf(source);
		if (selectedHandler != null)
			selectedHandler.onSelect(this);

	}

	private Selected selectedHandler;

	public void connect(Selected handler) {
		selectedHandler = handler;
	}

	interface Selected {
		public void onSelect(List source);
	}

}
